import { defineStore } from 'pinia'
import { ref, computed } from 'vue'

export const useCounterStore = defineStore(
  'counter',
   () => {
  // 定义 state
  const count = ref(0)
  const info = ref({
    name: '张三',
    age: 18,
    hobby: '打篮球'
  })

  // 定义 getters
  const double = computed(() => count.value * 2)

  // 定义 actions 方法
  const increment = () => {
    count.value++
  }

  const decrement = () => {
    count.value--
  }
  const decrementAge = () => {
    info.value.age ++
  }

  // 千万不要忘记这里要 return
  return { count, double, increment, decrement, info, decrementAge }
}, 
{
  //默认全部持久化存储 - 造成不必要的内存浪费
  // 开启后对 state 的数据读写都将持久化
  // unistorage: true, 

  // 部分缓存
  unistorage:{
    key: 'doctor-info' ,// LocalStorage中保存的key
    paths: ['count', 'info'] // 数组中是状态变量的属性名， 在里面的会自动持久化存储
  }
})